package stanaGUI;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.DateFormatter;

import Stana.hierarchicalClustering.PriceQuantityCluster;

public class DBOperations {
	ListSelectionModel listSelectionModel;
	Connection con; // Database connection
	String sqlQuery = "";
	int numRecordsReturned = 0;
	int numRows;
	JCheckBox[][] fieldsCheckBox;
	int numOfFields = 0;
	int numOfTables = 0;
	int numOfTableFields[];
	String[] dataObj1;
	String[] dataObj2;
	String[] dataObj3;
	String[] dataObj4;
	String[] dataObj5;
	String[] dataObj6;
	String[] dataObj7;
	String[] dataObj8;
	String[] dataObj9;

	int transactionId;
	String productName;
	String prefUnit;
	double costQuantity;
	double cost;
	double discountPercentage;
	double discountPrice;
	double price;
	String salesDate; 
	static boolean superMarkSelected = false;
	static boolean doListTablesNotDone = false;
	String[][] fieldsSelected;
	String[] tablesSelected;
	static int index = 0;
	JTextArea output;
	JList databaseList;
	JList fieldData;
	JList tableList;
	JList[] fieldList;
	public void getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql://localhost/SuperMarket", "root", "root");

		} catch(Exception e){
			e.printStackTrace();
		}
	}
	public void closeConnection() {
		try {
			con.close();
		} catch(Exception e){
			e.printStackTrace();
		}
	}

	public void executeQuery(int numData) {

		Statement stat;
		try {
			stat = con.createStatement();
			ResultSet rst=stat.executeQuery(sqlQuery);
			int j = 0;
			rst.last();
			numRows = rst.getRow();
			rst.beforeFirst();
			dataObj1 = new String[numRows];
			dataObj2 = new String[numRows];
			dataObj3 = new String[numRows];
			dataObj4 = new String[numRows];
			dataObj5 = new String[numRows];
			dataObj6 = new String[numRows];
			dataObj7 = new String[numRows];
			dataObj8 = new String[numRows];
			dataObj9 = new String[numRows];


			while(rst.next())
			{
				// Decide upon the number of queries
				for (int i = 1; i <= numData; i++) {
					if (i == 1) {
						dataObj1[j] = rst.getObject(i).toString();
					} else if (i == 2) {
						dataObj2[j] = rst.getObject(i).toString(); 
					} else if (i == 3) {
						dataObj3[j] = rst.getObject(i).toString(); 
					}else if (i == 4) {
						dataObj4[j] = rst.getObject(i).toString(); 
					}else if (i == 5) {
						dataObj5[j] = rst.getObject(i).toString(); 
					}else if (i == 6) {
						dataObj6[j] = rst.getObject(i).toString(); 
					}else if (i == 7) {
						dataObj7[j] = rst.getObject(i).toString(); 
					}else if (i == 8) {
						dataObj8[j] = rst.getObject(i).toString(); 
					}else if (i == 9) {
						dataObj9[j] = rst.getObject(i).toString(); 
					}
				}
				j++;
			}
			numRecordsReturned = j;
			// Store the result in Database list
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public int getTransactionId() {
		return transactionId;
	}
	public void setTransactionId(int transactionId) {
		this.transactionId = transactionId;
	}
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public String getPrefUnit() {
		return prefUnit;
	}
	public void setPrefUnit(String prefUnit) {
		this.prefUnit = prefUnit;
	}
	public double getCostQuantity() {
		return costQuantity;
	}
	public void setCostQuantity(double costQuantity) {
		this.costQuantity = costQuantity;
	}
	public double getCost() {
		return cost;
	}
	public void setCost(double cost) {
		this.cost = cost;
	}
	public double getDiscountPercentage() {
		return discountPercentage;
	}
	public void setDiscountPercentage(double discountPercentage) {
		this.discountPercentage = discountPercentage;
	}
	public double getDiscountPrice() {
		return discountPrice;
	}
public void setDiscountPrice(double discountPrice) {
		this.discountPrice = discountPrice;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getSalesDate() {
		return salesDate;
	}
	public void setSalesDate(String salesDate) {
		this.salesDate = salesDate;
	}
	public void doListFields(final String[] tablesSelected) {
		// Create a frame.
		final JFrame frame = new JFrame("List Of Fields");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final DBOperations db = new DBOperations();
		db.getConnection();
		db.numOfTables = numOfTables;
		JButton buttonToSubmit = new JButton("Submit");
		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				frame.dispose();
				db.doListFieldsData(tablesSelected);
			}
		};
		buttonToSubmit.addActionListener(actionListener);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,1));
		JList data = new JList();
        JPanel listContainer = new JPanel(new GridLayout(0,1));
        listContainer.setBorder(BorderFactory.createTitledBorder("Fields List"));
        db.fieldList = new JList[numOfTables];
		for (int i = 0 ; i < numOfTables; i++) {
			db.sqlQuery = "show Columns from " + tablesSelected[i] ;
			db.executeQuery(1);
			System.out.println(" the SQL Query " + db.sqlQuery);
			JLabel TableName =  new JLabel("Table " + tablesSelected[i]);
			panel.add(TableName);
			db.fieldList[i] = new JList(db.dataObj1);
		    db.fieldList[i].setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	        db.fieldList[i].addListSelectionListener(db.new fieldListSelectionHandler());
	        db.fieldList[i].setVisibleRowCount(5);
	        JScrollPane listPane = new JScrollPane(db.fieldList[i]);
	        listContainer.add(listPane);
	        panel.add(listContainer);
		}
		db.closeConnection();
		// We have to add the listeners
		frame.add(panel, BorderLayout.CENTER);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(300,30));
		buttonPanel.add(buttonToSubmit);
		frame.add(buttonPanel,BorderLayout.SOUTH);

		db.closeConnection();
		frame.setSize(300, 300);
		frame.setVisible(true);	    	
	}

	public void doListProcessFields(final String[] tablesSelected) {
		// Create a frame.
		final JFrame frame = new JFrame("Products List");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final DBOperations db = new DBOperations();	
		db.getConnection();
		db.numOfTables = numOfTables;
		JButton buttonToSubmit = new JButton("Submit");
		// Add the TextField for the StartDate and EndDate for analysis
		JPanel datePanel = new JPanel(null);
		datePanel.setBounds(0, 0, 200, 170);        
		datePanel.setPreferredSize(new Dimension(200, 170));     
		JLabel Heading = new JLabel("Enter the Start and End Date :");
		Heading.setBounds(25, 55,175, 20);
		datePanel.add(Heading);
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");        
		DateFormatter df = new DateFormatter(format);        
		final JFormattedTextField StartDate = new JFormattedTextField(df);		
		StartDate.setColumns(10);
		StartDate.setValue(new Date());
		JLabel start = new JLabel("Start Date     :");
		start.setBounds(25, 95, 85, 20);        
		StartDate.setBounds(125, 95, 85, 20);        
		datePanel.add(start);        
		datePanel.add(StartDate);        

		JLabel end = new JLabel("End   Date    :");
		final JFormattedTextField EndDate = new JFormattedTextField(df);		
		EndDate.setColumns(10);
		EndDate.setValue(new Date());
		end.setBounds(25, 135, 85, 20);        
		EndDate.setBounds(125, 135, 85, 20);        
		datePanel.add(end);
		datePanel.add(EndDate);

		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				frame.dispose();
				db.doProductDataList(db.fieldsSelected, StartDate.getText(),EndDate.getText());
			}
		};
		buttonToSubmit.addActionListener(actionListener);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,1));
		panel.setBounds(0, 0, 300, 150);        
		
		JList data = new JList();
        JPanel listContainer = new JPanel(new GridLayout(0,1));
    	JLabel prodHead = new JLabel("Select Only two products For Analysis :");
		prodHead.setBounds(25, 55,175, 20);
		panel.add(prodHead);
	
        listContainer.setBorder(BorderFactory.createTitledBorder("Products List"));
        db.fieldList = new JList[numOfTables];
		for (int i = 0 ; i < numOfTables; i++) {
			db.sqlQuery = "select Productname from  " + tablesSelected[i] ;
			db.executeQuery(1);
			System.out.println(" the SQL Query " + db.sqlQuery);
			JLabel TableName =  new JLabel("Table " + tablesSelected[i]);
			panel.add(TableName);
			db.fieldList[i] = new JList(db.dataObj1);
		    db.fieldList[i].setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	        db.fieldList[i].addListSelectionListener(db.new productListSelectionHandler());
	        db.fieldList[i].setVisibleRowCount(5);
	        JScrollPane listPane = new JScrollPane(db.fieldList[i]);
	        listContainer.add(listPane);
	        panel.add(listContainer);
		}
		db.closeConnection();
		// Basic validation should be there
		// We have to add the listeners
		frame.add(datePanel, BorderLayout.NORTH);
		frame.add(panel, BorderLayout.CENTER);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(100,35));
		buttonPanel.add(buttonToSubmit);
		frame.add(buttonPanel,BorderLayout.SOUTH);

		db.closeConnection();
		frame.setSize(300, 500);
		frame.setVisible(true);	    	
	}
	
	
	
	
	public void doListFieldsData(String[] tablesSelected) {
		// Create a frame.
		final JFrame frame = new JFrame("List Of Field Data");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final DBOperations db = new DBOperations();
		db.getConnection();
		JPanel panel = new JPanel(new GridLayout(0, 2));
		String query = "select " ;
		int numFieldsQuery = 0;
		for (int i = 0; i < numOfTables; i++) {
			for (int j = 0;j < numOfTableFields[i]; j++) {
				numFieldsQuery++;
				if ((j == numOfTableFields[i] - 1) && (i == numOfTables-1)) 
					query += tablesSelected[i] + "." + fieldsSelected[i][j];
				else
					query += tablesSelected[i] + "." + fieldsSelected[i][j] + ",";
			}
		}

	query += " from ";
	for (int i = 0; i < numOfTables; i++) {
		if (i == numOfTables-1 )
			query += tablesSelected[i] + ";";
		else	
			query += tablesSelected[i] + ",";
	}
	final String[] heading = new String[numFieldsQuery];
	int headIdx = 0;
	for (int i = 0; i < numOfTables; i++) {
		for (int j = 0;j < numOfTableFields[i]; j++) {
				heading[headIdx] = fieldsSelected[i][j];
				headIdx++;
		}
	}
	
	db.sqlQuery = query;
	System.out.println(" query " + query);
	db.executeQuery(numFieldsQuery);
	String[][] data = new String[db.numRecordsReturned][numFieldsQuery];
	for (int i = 0; i < db.numRecordsReturned; i++) {
		for (int j = 0; j < numFieldsQuery; j++) {
			if (j == 0)
				data[i][j] = db.dataObj1[i];
			if (j == 1)
				data[i][j] = db.dataObj2[i];
			if (j == 2)
				data[i][j] = db.dataObj3[i];
			if (j == 3)
				data[i][j] = db.dataObj4[i];
			if (j == 4)
				data[i][j] = db.dataObj5[i];
			if (j == 5)
				data[i][j] = db.dataObj6[i];
			if (j == 6)
				data[i][j] = db.dataObj7[i];
			if (j == 7)
				data[i][j] = db.dataObj8[i];
			if (j == 8)
				data[i][j] = db.dataObj9[i];


		}
	}
	// create a table format and give the results
	final JTable dataSet = new JTable(data,heading);
	panel.setLayout(new GridLayout(0,1));
	dataSet.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
	ListSelectionModel rowSM = dataSet.getSelectionModel();
    rowSM.addListSelectionListener( new ListSelectionListener() {
    		public void valueChanged(ListSelectionEvent e){
    			int myRow = dataSet.getSelectedRow();
    			for (int i = 0; i < heading.length; i++) {
    			String field1 = (String) dataSet.getValueAt(myRow, i);
    			System.out.println(" field 1 " + field1);
    			}
    		}
    });
    
    JScrollPane listPane = new JScrollPane(dataSet);
    JPanel listContainer = new JPanel(new GridLayout(0,1));
    listContainer.setBorder(BorderFactory.createTitledBorder("Fields Value"));
    listContainer.add(listPane);
	panel.add(listContainer);

	ActionListener actionListener = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			frame.dispose();
			try {
				MainWindow frame = new MainWindow();
				frame.setVisible(true);
			} catch (Exception exceptionMain) {
				exceptionMain.printStackTrace();
			}
		}
	};
	JPanel buttonPanel = new JPanel();
	buttonPanel.setPreferredSize(new Dimension(300,30));
	JButton submit = new JButton("Submit");
	submit.addActionListener(actionListener);
	buttonPanel.add(submit);
	db.closeConnection();
	frame.add(panel, BorderLayout.CENTER);
	frame.add(buttonPanel, BorderLayout.SOUTH);
	db.closeConnection();
	frame.setSize(300, 300);
	frame.setVisible(true);	    
	}

	public void doProductDataList(String[][] fieldsSelected, String startDate, String endDate) {

		String sqlQuery1 = "";
		String sqlQuery2 = "";
		if (fieldsSelected[0][0] != null) {
			sqlQuery1 = 
				"select ProductName, SalesDate, CostQuantity, Price from Transaction where SalesDate between " + "\'" + 
						startDate + "\'" + " and " + "\'" + 
						endDate + "\'" + " and productName=" + "\'" + fieldsSelected[0][0] + "\'";
		} 
		if(fieldsSelected[0][1] != null) {
			sqlQuery2 = 
					"select ProductName, SalesDate, CostQuantity, Price from Transaction where SalesDate between " + "\'" + 
							startDate + "\'" + " and " + "\'" + 
							endDate + "\'" + " and productName=" + "\'" + fieldsSelected[0][1] + "\'";

		}
		// Now execute the query and give in an array format and get the output in a list
		// use that process.
		final JFrame frame = new JFrame("Data getting processed");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final DBOperations db = new DBOperations();
		db.getConnection();
		// check How output comes.
		db.sqlQuery = sqlQuery1;
		db.executeQuery(4);
		int numRecords1 = 0;
		int numRecords2 = 0;
		String[][] data = new String[db.numRecordsReturned][4];
		String[][] data1 = new String[db.numRecordsReturned][4];

		numRecords1 = db.numRecordsReturned;
		for (int i = 0; i < db.numRecordsReturned; i++) {
			for (int j = 0; j < 4; j++) {
				if (j == 0)
					data[i][j] = db.dataObj1[i];
				if (j == 1)
					data[i][j] = db.dataObj2[i];
				if (j == 2)
					data[i][j] = db.dataObj3[i];
				if (j == 3)
					data[i][j] = db.dataObj4[i];
			}
		}
		if (sqlQuery2 != null) {
			db.sqlQuery = sqlQuery2;
			db.executeQuery(4);
			data1 = new String[db.numRecordsReturned][4];
			numRecords2 = db.numRecordsReturned;
			for (int i = 0; i < db.numRecordsReturned; i++) {
				for (int j = 0; j < 4; j++) {
					if (j == 0)
						data1[i][j] = db.dataObj1[i];
					if (j == 1)
						data1[i][j] = db.dataObj2[i];
					if (j == 2)
						data1[i][j] = db.dataObj3[i];
					if (j == 3)
						data1[i][j] = db.dataObj4[i];
				}
			}
		}
		int totQuantity = numRecords1 + numRecords2;
		String[][] dataOutput = new String[totQuantity][4];

		try {
			FileOutputStream fstream = new FileOutputStream("C:\\Challiz\\tempDB.txt");
			// Get the object of DataInputStream
			DataOutputStream out = new DataOutputStream(fstream);
			BufferedWriter br = new BufferedWriter(new OutputStreamWriter(out));
			for (int i = 0; i < numRecords1; i++) {
					// Write the output in the file also
				br.write(data[i][0] + "," + data[i][1] + "," + data[i][2] + "," + data[i][3]+"\r\n");
				dataOutput[i][0] = data[i][0];
				dataOutput[i][1] = data[i][1];
				dataOutput[i][2] = data[i][2];
				dataOutput[i][3] = data[i][3];
			}
			int dataOutputIdx = 0;
			for (int i = 0; i < numRecords2; i++) {
				// Write the output in the file also
				br.write(data1[i][0] + "," + data1[i][1] + "," + data1[i][2] + "," + data1[i][3]+ "\r\n");
				dataOutputIdx = i+numRecords1;
				dataOutput[dataOutputIdx][0] =  data1[i][0];
				dataOutput[dataOutputIdx][1] =  data1[i][1];
				dataOutput[dataOutputIdx][2] =  data1[i][2];
				dataOutput[dataOutputIdx][3] =  data1[i][3];
			}
			br.close();
		} catch (Exception e) {
			System.out.println(" the TempDB write operation error in DBOperations");
			System.out.println(e.getMessage());
		}
		
		final String[] heading = new String[4];

		heading[0] = "ProductName";
		heading[1] = "SalesDate";
		heading[2] = "CostQuantity";
		heading[3] = "Price";
		// create a table format and give the results
		final JTable dataSet = new JTable(dataOutput,heading);
		JPanel panel = new JPanel();
		panel.add(dataSet);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(300,30));
		JButton submit = new JButton("Submit");
		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				frame.dispose();
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception exceptionMain) {
					exceptionMain.printStackTrace();
				}
			}
		};
		
		submit.addActionListener(actionListener);
		buttonPanel.add(submit);
		db.closeConnection();
		frame.add(panel, BorderLayout.CENTER);
		frame.add(buttonPanel, BorderLayout.SOUTH);
		db.closeConnection();
		frame.setSize(300, 300);
		frame.setVisible(true);	    
	
	}
	
	
	
	public void doListTables(String database) {
		
		// Create a frame.
		final JFrame frame = new JFrame("List Of Tables");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final DBOperations db = new DBOperations();
		db.getConnection();
		JPanel panel = new JPanel(new GridLayout(0, 1));
		db.sqlQuery = "show Tables from " + database ;
		db.executeQuery(1);
		db.tableList = new JList(db.dataObj1);
        db.tableList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        db.tableList.addListSelectionListener(db.new TableListSelectionHandler());
        db.tableList.setVisibleRowCount(1);
        
        JScrollPane listPane = new JScrollPane(db.tableList);
        JPanel listContainer = new JPanel(new GridLayout(0,1));
        listContainer.setBorder(BorderFactory.createTitledBorder("Tables List"));
        listContainer.add(listPane);
		panel.add(listContainer);
		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				frame.dispose();
				// Here if we process more than 1 table this will change to 
				// db.doListFields(db.tablesSelected)
				db.doListProcessFields(db.tablesSelected);
			}
		};
		JPanel buttonPanel = new JPanel();
		buttonPanel.setPreferredSize(new Dimension(300,30));
		JButton buttonToSubmit = new JButton("Submit");
		buttonToSubmit.addActionListener(actionListener);
		buttonPanel.add(buttonToSubmit);
		db.closeConnection();
		// We have to add the listeners
		frame.add(panel, BorderLayout.CENTER);
		frame.add(buttonPanel,BorderLayout.SOUTH);
		db.closeConnection();
		frame.setSize(300, 300);
		frame.setVisible(true);	    	

	}

	public void doListDataBases(final DBOperations db) {
		db.getConnection();
		// Display the database name
		final JFrame frame = new JFrame("List Of Databases");
		Container pane = frame.getContentPane();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		db.sqlQuery = "show databases";
		db.executeQuery(1);
	    db.databaseList = new JList(db.dataObj1);
        db.databaseList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        db.databaseList.addListSelectionListener(db.new DatabaseListSelectionHandler());
        db.databaseList.setVisibleRowCount(8);
        
        JScrollPane listPane = new JScrollPane(db.databaseList);
        JPanel listContainer = new JPanel(new GridLayout(0,1));
        listContainer.setBorder(BorderFactory.createTitledBorder("DataBase List"));
        listContainer.add(listPane);
        panel.add(listContainer);
       //        panel.add(buttonToSubmit);
		db.closeConnection();
		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				frame.dispose();
	        	db.doListTables(db.databaseList.getSelectedValue().toString());
			}
		};
		panel.setPreferredSize(new Dimension(300,250));
		pane.add(panel, BorderLayout.CENTER);
		JPanel footer = new JPanel();
		footer.setPreferredSize(new Dimension(300,30));
        JButton buttonToSubmit = new JButton("Submit");
        footer.add(buttonToSubmit);
        frame.add(footer, BorderLayout.SOUTH);
		buttonToSubmit.addActionListener(actionListener);
		db.closeConnection();
		frame.setSize(300, 300);
		frame.setVisible(true);

	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final DBOperations db = new DBOperations();
		db.doListDataBases(db);
	}

	private class productListSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
         	numOfFields = 0;
        	for (int i = 0; i < numOfTables; i++) {
        		if (numOfFields < fieldList[i].getSelectedValues().length)
        			numOfFields = 	fieldList[i].getSelectedValues().length;
        		
        	}
        	fieldsSelected = new String[numOfTables][numOfFields];
        	numOfTableFields = new int[numOfTables];
        	for (int i=0; i < numOfTables; i++) {
        		 Object[] data = fieldList[i].getSelectedValues();
        		 numOfTableFields[i] = data.length;
        		 for (int j = 0; j < data.length; j++ ) {
        			fieldsSelected[i][j] = data[j].toString();
        		 }
        	}
        }	
	}	
	
	
	
	private class fieldListSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
         	numOfFields = 0;
        	for (int i = 0; i < numOfTables; i++) {
        		if (numOfFields < fieldList[i].getSelectedValues().length)
        			numOfFields = 	fieldList[i].getSelectedValues().length;
        		
        	}
        	fieldsSelected = new String[numOfTables][numOfFields];
        	numOfTableFields = new int[numOfTables];
        	for (int i=0; i < numOfTables; i++) {
        		 Object[] data = fieldList[i].getSelectedValues();
        		 numOfTableFields[i] = data.length;
        		 for (int j = 0; j < data.length; j++ ) {
        			fieldsSelected[i][j] = data[j].toString();
        		 }
        	}
        }	
	}	
	

	private class TableListSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
        	int num = tableList.getSelectedValues().length;
        	numOfTables = num;
        	Object[] data = new Object[num];
        	data = tableList.getSelectedValues();
        	tablesSelected = new String[num];
        	for (int i = 0; i < num; i++) {
        		tablesSelected[i] = data[i].toString();
        	}
        }	
	}
	
	class DatabaseListSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
           	System.out.println(" the Number of components " + databaseList.getSelectedValue());
        }	
    }
	
	
}
